<?php
namespace Kuoqi\DbDriver;
use \Kuoqi\Cache;
use Kuoqi\Error;

//pdo版本的数据库驱动对象
class PdoDriver extends SqlDriver implements DbInterface{
	
	//核心执行query方法
	public function query($sql){
	    \Kuoqi\Logs::listenStart('query');
		$query = self::$DB_DRIVER_QUERY->query($sql);
		if($query===false){
			$info = self::$DB_DRIVER_QUERY->errorInfo();
			if($info[1]=='2006'){
                self::$DB_DRIVER_QUERY = null;//清空当前的数据库缓存器
				//尝试重新连接服务器
				$this->connect();
				return $this->query($sql);
			}else{
				$msg = "[PdoDriver][{$info[2]}] on <span style='color: #333;text-decoration: line-through;'>{$sql}</span>";
                \Kuoqi::log($msg,'error');
				throw new Error($msg);
			}
		}
		//记录query的运行时间
		\Kuoqi::log($sql,'query',\Kuoqi\Logs::listenEnd('query'));
		return $query;
	}
	
	//数据库结果类-简单查询
	public function find($andWhere = array()){
		$sqlobj = new Sql\Sql($this);
		$sql = $sqlobj->scale('find');
		if($this->_BACK_SQL){
			return $sql;
		}
		
		//启用缓存的步骤
		if($this->hasCache&&$data = Cache::getCache($sql,'/database',false)){
			return $data;
		}
		
		//使用查询语句
		$query = $this->query($sql);
		
		$data = $query->fetch(\PDO::FETCH_ASSOC);
		if($this->hasCache){
			Cache::setCache($sql,$data,$this->hasCache,'/database',false);
            $this->hasCache = 0;//变更缓存的时间信息
		}
		
		return $data;
	}

    /**
     * 数据库结果类-简单多条数据查询
     * @param array $andWhere
     * @return mixed|null
     * @throws Error
     */
	public function select($andWhere = array()){
		$sqlobj = new Sql\Sql($this);
		$sql = $sqlobj->scale('select');
		if($this->_BACK_SQL){
			return $sql;
		}
		//启用缓存的步骤
		if($this->hasCache&&$data = Cache::getCache($sql,'/database',false)){
			return $data;
		}
		//使用查询语句
		$query = $this->query($sql);
		$data = $query->fetchAll(\PDO::FETCH_ASSOC);
		if($this->hasCache){
			Cache::setCache($sql,$data,$this->hasCache,'/database',false);
            $this->hasCache = 0;//变更缓存的时间信息
		}
		
		return $data;
	}
	
	//数据库结果类-简单增加
	public function insert($data = array()){
		$this->andData($data);
		$sqlobj = new Sql\Sql($this);
		$sql = $sqlobj->scale('insert');
		if($this->_BACK_SQL){
			return $sql;
		}
		$query = $this->query($sql);
		$data = self::$DB_DRIVER_QUERY->lastInsertId();
		return $data;
		
	}
	
	
	//数据库结果类-简单更新
	public function update($data = array()){
		$this->andData($data);
		$sqlobj = new Sql\Sql($this);
		$sql = $sqlobj->scale('update');
		if($this->_BACK_SQL){
			return $sql;
		}
		$data = self::$DB_DRIVER_QUERY->exec($sql);
		return $data;
	}
	
	//数据库结果类-简单查询
	public function delete($andWhere = array()){
		$this->andWhere($andWhere);
		$sqlobj = new Sql\Sql($this);
		$sql = $sqlobj->scale('delete');
		$data = self::$DB_DRIVER_QUERY->exec($sql);
		return $data;
	}

	
	//数据库结果类-简单的条数查询
	public function count(){
		$sqlobj = new Sql\Sql($this);
		$sql = $sqlobj->scale('count');
        if($this->_BACK_SQL){
            return $sql;
        }
		$query = $this->query($sql);
		return $query->fetchColumn();
	}
	
	//获取所有列名的详细信息
	//这种字段一般都不会在修改了，所以，如果在非调试模式直接使用缓存即可
	//不受缓存字段的控制
	public function getColumns(){
		$sqlobj = new Sql\Sql($this);
		$sql = $sqlobj->scale('columns');
		if($this->_BACK_SQL){
			return $sql;
		}
		//启用缓存的步骤
        if($this->hasCache&&$data = Cache::getCache($sql,'/database',false)){
			return $data;
		}
		$query = $this->query($sql);
		$data = $query->fetchAll(\PDO::FETCH_ASSOC);
        if($this->hasCache){
            Cache::setCache($sql,$data,$this->hasCache,'/database',false);
            $this->hasCache = 0;//变更缓存的时间信息
        }
		return $data;
	}
	
	public function startTrans()
	{
	   return self::$DB_DRIVER_QUERY->beginTransaction();
    }

    public function rollBack()
	{
        return self::$DB_DRIVER_QUERY->rollBack();
    }

    public function commit()
	{
        return self::$DB_DRIVER_QUERY->commit();
    }

    public function __destruct()
    {
        // TODO: Implement __destruct() method.
    }

}